﻿
namespace GameStudio.IDE.Controls
{
    using System.Windows;
    using System.Windows.Controls;
    using GameStudio.Common;
    using GameStudio.IDE.Common.Events;
    using Microsoft.Practices.ServiceLocation;
    using Utils.EventAggregator;
    using GameStudio.IDE.Common;
    using GraphicsDesigner.Controls;
    using AvalonDock;
    using System.Collections.Generic;
    using System;
    using GameStudio.CreateProject;

    [TemplatePart(Name = "uxMDITab", Type = typeof(DocumentPane))]
    public class TabContainer : Control
    {
        private DocumentPane uxMDITab;
        public static Dictionary<string, DocumentContent> TabItemsDictionary;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            uxMDITab = (DocumentPane)GetTemplateChild("uxMDITab");
            TabItemsDictionary = new Dictionary<string, DocumentContent>();
        }

        public TabContainer()
        {
            //initialize templates
            ProjectCreateInfo projectTemplate = new ProjectCreateInfo();
            System.IO.Directory.CreateDirectory(ProjectCreateInfo.TmpLocation);
            projectTemplate.CreateTemplate();
            Application.Current.Resources["ThemeDictionary"] = new ResourceDictionary();
            //ThemeFactory.ChangeTheme(new Uri("/AvalonDock.Themes;component/themes/dev2010.xaml", UriKind.RelativeOrAbsolute));
            //ThemeFactory.ResetTheme();

            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

            eventAggregator.GetEvent<CreateProjectEvent>().Subscribe(AddNewTab);

            eventAggregator.GetEvent<AddNewItemEvent>().Subscribe(OnAddNewItem);
            eventAggregator.GetEvent<OpenItemEvent>().Subscribe(OpenItem);

            eventAggregator.GetEvent<SelectItemSolutionExplorerEvent>().Subscribe(FocusOnTab);

        }

        private void AddNewTab(GameStudio.IDE.Common.Project project)
        {
            //TabItem item = new TabItem();
            //item.Header = project.Name;
            //this.uxMDITab.Items.Add(item);
            DocumentContent item = new DocumentContent();
            item.Title = project.Name;
            this.uxMDITab.Items.Add(item);

        }

        private void OnAddNewItem(Item item)
        {
            DocumentContent tabItem = new DocumentContent();

            if (item.Type == "Class")
            {
                ControlTemplate template = (ControlTemplate)FindResource("CodeEditorControlTemplate");
                tabItem.Content = new CodeEditorControl() { Template = template };
            }
            else if (item.Type == "Model")
            {
                ControlTemplate template = (ControlTemplate)FindResource("DesignerControlTemplate");
                tabItem.Content = new DesignerControl(item.Name, item.Path) { Template = template };

                var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
                eventAggregator.GetEvent<FocusOnGraphicsTabEvent>().Publish("");
            }

            tabItem.Title = item.Name; ;

            this.uxMDITab.Items.Add(tabItem);
            this.uxMDITab.SelectedItem = tabItem;
            TabItemsDictionary.Add(item.Name, tabItem);
        }

        private void OpenItem(string str)
        {
            DocumentContent tabItem = new DocumentContent();

            ControlTemplate template = (ControlTemplate)FindResource("CodeEditorControlTemplate");
            tabItem.Content = new CodeEditorControl() { Template = template };

            tabItem.Title = str;

            tabItem.Focus();
            this.uxMDITab.Items.Add(tabItem);
        }

        private void FocusOnTab(string tabName)
        {
            if (TabItemsDictionary.ContainsKey(tabName))
            {
                uxMDITab.SelectedItem = TabItemsDictionary[tabName];
            }
        }
    }
}
